import {getTaskInfoReq, submitYQMReq,} from "../apis/goApis";
import {handleResp} from "../utils/commonfunc";
import {Toast} from "antd-mobile";
import {formatDate} from "../utils/dateService";

export const task = {

    state: {
        taskList: [],
        finished: [],
    },

    reducers: {
        setTodayTask(state, payload) {
            const {
                form,
                data,
            } = payload

            const taskList = [...form]
            // eslint-disable-next-line array-callback-return
            data.map(task => {

                const Id = task.Style
                const day = task.Day
                const now = new Date()
                const today = formatDate(now)
                const index = taskList.findIndex(t => t.Id === Id)
                if (taskList[index].Allnum > 0) {
                    taskList[index].finished = task
                } else {
                    if (day === today) {
                        taskList[index].finished = task
                    }
                }
            })

            return {
                ...state,
                taskList: form,
                // finished: data,
            }
        }
    },

    effects: (dispatch) => ({

        doGetTaskInfo: async (payload, rootState) => {
            const resp = await getTaskInfoReq(payload)
            handleResp(resp, dispatch, (data) => {
                dispatch.task.setTodayTask(data)
            })
        },

        doSubmitYQM: async (payload, rootState) => {
            const resp = await submitYQMReq(payload)
            handleResp(resp, dispatch, (data) => {
                if (data.ok === 1) {
                    Toast.info('邀请码填写成功')
                    dispatch.user.fetchUserDetail({uid: payload.uid,})
                }
            })
        }

    })

}
